package service

import (
	"github.com/gin-gonic/gin"
	"hanlinyuan_qa/model"
	"hanlinyuan_qa/util"
	"net/http"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	println("用户名：", username)
	var data []model.User
	model.GetDB().Model(data).Where("user_name=?", username).Find(&data)
	if len(data) == 0 {
		c.JSON(http.StatusOK, gin.H{
			"data": "",
			"msg":  "未找到用户",
			"code": 0,
		})
		return
	}
	if data[0].Password != password {
		c.JSON(http.StatusOK, gin.H{
			"data": "",
			"msg":  "密码不正确",
			"code": 0,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": util.GetToken(data[0].UserID),
		"msg":  "登录成功！",
		"code": 1,
	})
}
